<!-- 任务分配弹窗 -->

<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="visible" custom-class="dialog_default">

    <el-form :label-width="$globalCss.formLabelWidth_default" v-loading="loading" :model="form" :rules="rules"
      ref="TaskAssignForm">
      <el-form-item :label="$tt('text_has_select_order')">
        <ex_super_table :model="_intoListOption()" :data="itemList" max-height="300" />

      </el-form-item>
      <el-form-item :label="$tt('label_assign_user')" prop="select">
        <el-radio-group v-model="form.select" class="w_fill">
          <el-table ref="PersonTable" :data="peopleList" border max-height="400" @selection-change="_selectMultiple"
            @row-click="_rowClick">
            <el-table-column type="index" align="center" />
            <el-table-column type="selection" align="center" v-if="multiple" />
            <el-table-column align="center" v-else width="60">
              <template v-slot="{ row }">
                <el-radio style="margin-left: 10px;" :label="row" :key="row.userId">{{ "" }}</el-radio>
              </template>
            </el-table-column>


            <el-table-column prop="userName" :label="$tt('label_person_name')" align="center" />
            <el-table-column prop="userCode" :label="$tt('label_person_code')" align="center" />
          </el-table>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog_footer">
      <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
      <el-button type="primary" @click="submit">{{ $tt('label_submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'taskAssign',
  data() {
    return {
      radio: false,
      visible: false,

      loading: false,
      itemList: [],
      taskDetailTitle: "",
      taskDetailDialog: false,
      taskDetailLoading: false,
      taskDetailData: [],



      peopleList: [],
      form: {
        select: null,
      },
      rules: {
        select: [{
          required: true, trigger: 'blur', validator: (r, v, cb) => {
            if (this.multiple) {
              if (v instanceof Array && v.length > 0) return cb()
              return cb(this.$tt("tip_please_select_user"))
            } else {
              if (v != null) return cb()
              return cb(this.$tt("tip_please_select_user"))
            }
          },
        }]
      },
    }
  },
  props: {
    title: String,
    intoTableOption: {
      type: Array,
      default: () => []
    },

    // taskDetailDataFun: Function,
    // taskDetailTableModel: Array,
    multiple: Boolean,

  },
  methods: {
    _intoListOption() {
      let _list = _.cloneDeep(this.intoTableOption)
      let operateItem = _.find(_list, it => it.type == 'operate')
      if (operateItem) {
        operateItem.operate.push({
          label: 'label_remove', class: 'button_text_danger', click: (row, i, list) => {
            list.splice(i, 1)
            if (_.isEmpty(list)) this.hide()
          }
        })
      } else {
        _list.push({
          type: 'operate', operate: [
            {
              label: 'label_remove', class: 'button_text_danger', click: (row, i, list) => {
                list.splice(i, 1)
                if (_.isEmpty(list)) this.hide()
              }
            }
          ]
        })
      }
      return _list
    },
    _rowClick(row) {
      if (this.multiple) {
        this.$refs.PersonTable.toggleRowSelection(row)
      } else {
        this.form.select = row
      }
    },
    _selectMultiple(e) {
      if (!this.multiple) return
      this.form.select = e
    },
    _selectSingle(e) {
      if (this.multiple) return
      this.form.select = e
    },
    show(warehouseId, itemList) {
      this.form.select = null
      this.visible = true
      this.itemList = _.cloneDeep(itemList)
      this.$nextTick(() => {
        if (this.$refs.PersonTable) this.$refs.PersonTable.clearSelection()
        if (this.$refs.TaskAssignForm) this.$refs.TaskAssignForm.clearValidate()

      })

      let userUrl = '/ums-api/user/UserInfoRpc/getUserDict.json'
      let list = this.$store.state.commonMapCache[userUrl] ? this.$store.state.commonMapCache[userUrl]["?"] : null

      if (list) {
        this.peopleList = list
      } else {
        this.loading = true
        this.$ajax({
          url: userUrl,
          method: 'post',
          success: (res) => {

            const list = (res.data && res.data.rows) || []
            this.peopleList = list
            this.$store.commit("pushCommonData", {
              group: userUrl,
              key: this.$queryStr({}),
              data: list
            })
          },
          failure: () => {
            this.peopleList = []
          },
          complete: () => {
            this.loading = false

          },
        })
      }


    },
    hide() {
      this.visible = false
    },
    submit() {

      this.$refs['TaskAssignForm'].validate((valid) => {
        if (valid) {
          this.$emit('afterSubmit', { select: this.form.select, list: this.itemList },)
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    },

  }
}
</script>
